var t = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
    return typeof t
  } :
  function(t) {
    return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t
  },
  e = getApp(),
  a = e.requirejs("core"),
  r = e.requirejs("check");
Page({
  data: {
    icons: e.requirejs("icons"),
    list: {},
    data: [],
    showPicker: false,
    pvalOld: [0, 0, 0],
    pval: [0, 0, 0],
    areas: [],
    noArea: true,
    submit: true,
    button_color: e.config.button_color,
    disabled:false,
    is_mention: 1,//1邮寄，2自提，3自选
    send_type: 'express',
    date: '',
    time: '17:00',
    formid: "",
    express_fee: 0,
  },

  // 自提、邮寄
  dispatchtype: function(e) {
    var data = a.pdata(e);
    this.setData(data);

    if (this.data.send_type == 'express' || this.data.send_type == 'endpay') {
        this.caculate(this.data.express_fee);
    } else {
        this.caculate(0);
    }
  },

  // 日期选择
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var get_time = a.time_str(e.detail.value + ' ' + this.data.time);
    var now_time = Date.parse(new Date()) / 1000;

    if (now_time > get_time) {
      a.error('不小于当前时间');
      return;
    }
    this.setData({
      date: e.detail.value
    })
  },

  // 时间选择
  bindTimeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },

  // 信息输入
  dataChange: function(t) {
    var e = this.data.data,
        a = this.data.list;
    switch (t.target.id) {
      case "buyer_message":  //留言
        e.buyer_message = t.detail.value;
        break;
      case "consigner":
        e.consigner = t.detail.value;
        break;
      case "phone":
        e.phone = t.detail.value;
        break;
    }
    this.setData({
      data: e,
      //list: a
    })
  },

  onLoad: function(options) {
    console.log("options",options);
    a.setting();
    var i = this,
        data = this.data,
        d = {};
    this.setData({
      options: options,
      button_color: getApp().config.button_color
    })

    a.get("user/About", {
      user_id: getApp().getCache("userinfo").uid
    }, function (t) {
      if (t.code == 0) {
        var send_type = i.data.send_type;
        if (t.info.is_mention == 2 || t.info.is_mention == 6) {
          send_type = 'selftake';
        } else if (t.info.is_mention == 4) {
          send_type = 'endpay';
        }

        i.setData({
          user_level: t.info.user_level,
          user_rebate: t.info.user_rebate,
          is_mention: t.info.is_mention,
          send_type: send_type
        })
      }
    })
    console.log("aaa",i.data);
    //获取用户地址
    a.get("Area/UserAddress", {
      uid: getApp().getCache("userinfo").uid
    }, function(t) {
      if (t.code == 0) {
        i.setData({
          address: t.info.address,
        })
        if (options.activity_order_type != 3) {
          //获取订单信息
          a.get("Bargain/GoodsInfo", {
            id: i.data.options.bargain_id
          }, function(t) {
            0 == t.code ? (
              i.setData({
                list: t.info.bargain_info,
                show: true,
              }),
              i.caculate_fee(i.data.options.bargain_id, i.data.options.total, function(fee) {
                console.log("fee",fee)
                i.caculate(fee)
              })
            ) : a.alert(t.msg)
          }, true)
        } else {
          var bargain_info = {
            'bargain_name': options.name,
            'original_price': options.current_price,
            'pic': options.pic,
            'goods_id': options.goods_id
          };
          i.setData({
              list: bargain_info,
              show: true,
          }),
          i.caculate_fee(i.data.options.bargain_id, i.data.options.total, function(fee) {
            i.caculate(fee)
          })
        }
      } else {
        a.alert(t.msg)
      }

    })

  },

  onShow: function() {
    var i = this,
      d = e.getCache("orderAddress");

    if (d) {
      this.setData({
        address: d,
      })
      i.caculate_fee(i.data.options.bargain_id, i.data.options.total, function(fee) {
        i.caculate(fee)
      })
    }
  },

  /**
   *返回触发事件
   * @param t 事件
   * @return array
   */
  toggle: function(t) {
    var e = a.pdata(t),
      i = e.id,
      d = e.type,
      r = {};
    r[d] = 0 == i || void 0 === i ? 1 : 0,
      this.setData(r)
  },

  // 运费
  caculate_fee: function(goods_id, num, obj) {
    var that = this,
        data = that.data.address;

    if (that.data.send_type == "selftake") {
      typeof obj == "function" && obj(0);
    } else {
      if (data.area != undefined && data.area > 0) {
        a.get('Market/UserFee', {
          goods_id: goods_id,
          num: num,
          area_id: data.area,
          type: 2
        }, function(t) {
          console.log("fee_info",t.info)
          if (t.code == 0) {
            that.setData({
              express_fee: t.info
            })
            typeof obj == "function" && obj(t.info);
          } else {
            a.alert(t.msg, function() {
              typeof obj == "function" && obj(0);
            });
          }
        })
      } else {
        typeof obj == "function" && obj(0);
      }
    }
  },

  //计算
  caculate: function(fee) {
    var e = this,
        a = e.data.address;
    //订单价
    a.order_money = e.data.options.current_price;
    //运费
    if (e.data.send_type == 'express' || e.data.send_type == "endpay") {
      a.shipping_money = fee;
    } else {
      a.shipping_money = 0;
    }
    // a.shipping_money = e.data.options.total * e.data.list.goods.shipping_fee;
    //实付价
    a.pay_money = parseFloat(parseInt(e.data.options.total) * parseFloat(e.data.options.current_price) + a.shipping_money).toFixed(2);
    //a.pay_money = parseFloat(e.data.options.total * (e.data.list.price - e.data.list.goods.shipping_fee) - discount_money);
    //a.pay_money = e.data.options.current_price;
    a.total = e.data.options.total;
    a.user_name = getApp().getCache("userinfo").nickName;
    a.buyer_id = getApp().getCache("userinfo").uid;
    // a.mch_id = e.data.list.mch_id;
    a.activity_order_type = e.data.options.activity_order_type ? e.data.options.activity_order_type : 0;
    a.bargain_id = e.data.options.bargain_id;
    e.setData({
      data: a
    })
  },

  // 提交订单
  formSubmit: function(v) {
    var e = this,
        t = this.data;

    if (t.send_type == 'selftake') {
      if (!t.data.consigner) {
        a.error('请填写姓名');
        return false;
      }
      if (!t.data.phone) {
        a.error('请填写电话');
        return false;
      }
      if (!t.date) {
        a.error('请选择自提日期');
        return false;
      }
      t.data.mention_time = a.time_str(t.date + ' ' + t.time);
      t.data.mailing_type = 2;
    } else if (t.send_type == 'endpay') {
      if (!t.address.phone || t.address.province == '') {
        a.error('请选择收货地址！');
        return false;
      }
      t.data.mailing_type = 4;
    } else {
      if (!t.address.phone || t.address.province == '') {
        a.error('请选择收货地址！');
        return false;
      }
      t.data.mailing_type = 1;
    }

    if (t.disabled) {
      return false;
    }

    e.setData({
      disabled: true
    })

    var formId = v.detail.formId;
    a.get("Bargain/CreateOrder", t.data, function(t) {
      if (0 == t.code) {
        //推送
        a.get('Wxpush/CreateOrderPush2', {
          order_id: t.info,
          formid: formId,
          uid: getApp().getCache("userinfo").uid,
        }, function (tt) {

          // console.log(tt)
          // console.log(22222222);
        })
        e.setData({
            submit: false,
            order_id: t.info,
            disabled: false,
        })
        // 订单金额为0时调用接口，支付成功
        if (e.data.data.pay_money == 0) {
          a.get("pay/updPayStatus", {
            order_id: t.info,
            pay_type: 5
          }, function(res) {
            if (res.code == 1) {
              wx.redirectTo({
                url: "/yb_mingpian/pages/kanjia/order/pay/index?id=" + t.info
              })
            } else {
              a.alert(res.msg);
              console.log("update_order",res);
            }
          })
        } else {
          wx.redirectTo({
            url: "/yb_mingpian/pages/kanjia/order/pay/index?id=" + t.info
          })
        }
      } else {
        e.setData({
          disabled: false
        })
        wx.hideLoading();
        a.alert(t.msg)
      }
    })
  },

  // 地址跳转
  url: function(t) {
    var e = a.pdata(t).url;
    wx.redirectTo({
      url: e
    })
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
  },

})